package com.itcrx.binarySearch;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author NoobMaster69  * @date 2024/5/28 0028 星期二 19:41
 */
public class TestBinarySearch {

    @Test
    public void test1() {
//        int[] a = {7, 13, 21, 30, 38, 44, 52, 53};
        int[] a = {1, 2, 4, 4, 4, 5, 6, 6,7};

        BinarySearch bs = new BinarySearch();
        System.out.println("索引：" + bs.binarySearchBasic(a, 4));
        System.out.println("索引：" + bs.binarySearchLeftmost1(a, 4));
    }

    @Test
    public void test2() {
        int[] a = {2, 5, 8};
        int target = 4;

        int i = Arrays.binarySearch(a, target);
        System.out.println("i = " + i);
        /*
          0 0 0 0
          2 0 0 0
          2 4 0 0
          2 4 5 8

          -2 = - 插入点 - 1
          -2 + 1 = - 插入点
         */

        if (i < 0) {
            int insertIndex = Math.abs(i + 1);  //求绝对值后的插入点
            int[] b = new int[a.length + 1];    //创造一个新数组
            System.arraycopy(a, 0, b, 0, insertIndex);  //数组拷贝 最后一个参数是要插入的数量
            b[insertIndex] = target;    //插入点的赋值
            System.arraycopy(a, insertIndex, b, insertIndex + 1, a.length - insertIndex);
            System.out.println(Arrays.toString(b));
        }

    }

    @Test
    public void test3() {
        int[] a = {1, 2, 4, 4, 4, 5, 6, 6,7};

        BinarySearch bs = new BinarySearch();
        System.out.println("索引：" + bs.binarySearchLeftmost1(a, 4));
    }

    @Test
    public void test5() {
//        int[] a = {7, 13, 21, 30, 38, 44, 52, 53};

        int[] a = {5,7,7,8,8,10};
        BinarySearch bs = new BinarySearch();
        System.out.println("索引：" + bs.tt(a, 6));
    }

}
